﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;

namespace YouHP.Pages.BBS
{
    public class ReplyEdit:TopicBasePage
    {
        
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected ReplyInfo ReplyData;
        
        protected new void HandleCommon()
        {
            if (UserData.Id <= 0)
            {
                this.AddErrLine("您还没有登录，请点击先登录！");
                this.MsgboxUrl = this.GetUrl("/User/Login.aspx");
                return;
            }
            UserInfo _User = UserBLL.Instance.GetUserInfo(UserData.Id);
            if ((_User.UserTypeId & 0x100) != 0)
            {
                this.AddErrLine("您已被在本站禁止发言");
                return;
            }
            int rid = RequestTools.GetInt("rid", 0);
            if (rid <= 0)
            {
                this.AddErrLine("无效的参数 rid ！");
                return;
            }

            ReplyData=ReplyBLL.Instance.GetReplyInfo(rid);
            if(ReplyData.Id<=0)
            {
                this.AddErrLine("此回复不存在！");
                return;
            }

            if(UserData.Id!=ReplyData.PostUserId)
            {
                this.AddErrLine("你没有权限操作！");
                return;
            }
        }

        protected void HandlePost()
        {
            ReplyInfo info = ValidateForm();
            if (ErrorCount > 0) return;
            bool succ = ReplyBLL.Instance.UpdateReply(info);
            if (!succ)
            {
                this.AddErrLine("操作失败！");
                return;
            }
            else
            {
                this.Redirect(string.Format("/BBS/Topic.aspx?gid={0}&tid={1}", TopicData.Groupid,ReplyData.TopicId));
            }
        }

        protected void HandleGet()
        {

        }

        protected ReplyInfo ValidateForm()
        {
            ReplyData.Body = RequestTools.GetFormString("tbxContent").Trim();
            if (ReplyData.Body.Length == 0)
            {
                this.AddErrLine("回复长度不能为空！");
            }
            else if (ReplyData.Body.Length > Config.ReplyMaxSize)
            {
                this.AddErrLine("回复长度不能大于 " + Config.ReplyMaxSize+"字符！");
            }
            ReplyData.UpdateTime = DateTime.Now;
            return ReplyData;
        }
    }
}